package com.comut.crud.ejb;

import com.comut.crud.bo.VehicleHandler;
import com.comut.crud.connection.MooveFleetConnectionManager;
import com.comut.crud.ejb.interfaces.local.IVehicleActionLocal;
import com.comut.crud.ejb.interfaces.remote.IVehicleActionRemote;
import com.comut.crud.pojo.mobile.VehicleDetail;
import com.comut.crud.pojo.mobile.VehicleInfo;
import com.comut.lib.connection.MongoDBConnectionManager;
import java.sql.Connection;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * Issue: IFL-1566
 *
 * @author Paulo Fournier
 */
@Stateless
@WebService
public class VehicleBean implements IVehicleActionRemote, IVehicleActionLocal {

    private static Logger _logger = Logger.getLogger(VehicleBean.class.getName());

    /**
     * Method to retrive the list of vehicle from a user
     * @param userId The user id
     * @param vehicleId The vehicle id
     * @return VehicleInfo[] The list of vehicles
     */
    @WebMethod
    public VehicleInfo[] getVehiclesForUserId(int userId) {
        VehicleInfo[] ret = null;
        Connection connection = null;
        VehicleHandler vehicleHandler;

        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            MongoDBConnectionManager mongodb = MongoDBConnectionManager.instanceFromSystemProperties();
            vehicleHandler = new VehicleHandler(connection, mongodb);
            ret = vehicleHandler.getVehiclesForUserId(userId);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            vehicleHandler = null;
        }

        return ret;
    }

    /**
     * Get the detail info from a vehicle
     * @param userId The user id
     * @param vehicleId The vehicle id
     * @return VehicleDetail The vehicle info
     */
    public VehicleDetail getVehicleDetail(int userId, int vehicleId) {
        VehicleDetail ret = null;
        Connection connection = null;
        VehicleHandler vehicleHandler;

        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            MongoDBConnectionManager mongodb = MongoDBConnectionManager.instanceFromSystemProperties();
            vehicleHandler = new VehicleHandler(connection, mongodb);
            ret = vehicleHandler.getVehicleDetail(userId, vehicleId);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            vehicleHandler = null;
        }

        return ret;
    }
}
